﻿using B.S.DataBase.MyProject.Domain.RBAC;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Menus;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Menus
{
    public class DelRoleMenuCommandHandler : IRequestHandler<DelRoleMenuCommand, APIResult<object>>
    {

        private readonly IBaseRepository<RoleMenu> rmRep;

        public DelRoleMenuCommandHandler(IBaseRepository<RoleMenu> rmRep)
        {
            this.rmRep = rmRep;
        }

        public async Task<APIResult<object>> Handle(DelRoleMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var rolemenus = request.RMIds.Select(x => new RoleMenu { RMId = x }).ToList();
            var res = rmRep.DeleteRemove(rolemenus).Result;
            if (res > 0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res.ToString();
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
            }
            return await Task.FromResult(result);
        }
    }
}
